#include <LPC22xx.h>
#include "Timer.h"

// 60Mhz PCLK (60Mhz/1)
#define TICKSPERSEC 60000000

void Timer::Init()
{
	T0PR = 0x00;	 // max speed
	T0TCR = 1;       // Timer0 Enable
	m_StartTime = 0;
}

void Timer::BeginPeriod()
{
	m_StartTime = T0TC;
}

bool Timer::HasElapsed(float time)
{
	unsigned int totalTimeTicks = time*TICKSPERSEC;	
	unsigned int currentTimeStamp = T0TC;
	unsigned int deltaTime = currentTimeStamp-m_StartTime;	

	if( deltaTime < totalTimeTicks ) return false;
	return true;
}

void Timer::WaitPeriod(float time)
{
	while( !HasElapsed(time) );
}

float Timer::ElapsedTime()
{
	unsigned int currentTimeStamp = T0TC;
	unsigned int deltaTime = currentTimeStamp-m_StartTime;	

	float elapsed = (float)deltaTime/TICKSPERSEC;
	
	return elapsed;
}

